// pagesProperty/pages/oa/worklog/add/index.js

var app = getApp(), fetchApi = require("../../../../../utils/httpPost.js")
, common_Js = require("../../../../../component/common.js");
import Dialog from '../../../../../component/vant/dialog/dialog';
import Toast from '../../../../../component/vant/toast/toast';

let editor;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    submitState:false,
    content:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  // 富文本编辑器内容变化时，同步更新表单字段内容 formData.content
  contentChange(e) {
    console.log('contentChange', e)
    let contentHtml = e.detail.html
    this.data.content = contentHtml;
    this.setData({
      content: contentHtml
    })
  },
  onEditorReady(){
    let that = this
    // 修改时，反显数据
    this.createSelectorQuery().select("#editor")
      .context((res) => {
        res.context.setContents({
          html: that.data.content,
        });
      })
      .exec();
  },
  submitOp(){
    if(this.data.content == '' || this.data.content == undefined){
      Toast('请输入日志内容')
      return;
    }
    if(this.data.submitState == true){
      Toast('正在提交请稍等...')
      return;
    }
    this.data.submitState = true;
    this.setData({
      submitState: true,
    })
    this.onHttpSubmit();
  },  
  onHttpSubmit: function () {
    var that = this;
    fetchApi.httpPost("/estate/add/work/log", {
      content: that.data.content,
    }, function (res) {
      console.log("获取上报日志返回数据:", res)
      
      if(res.data.code == 0){
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: -1,
          })
        },300)
      }else{
        Toast(res.data.msg);
      }

      that.data.submitState = false;
      that.setData({
        submitState: false,
      })

    }, function (a) {
      console.log("获取上报日志返回数据 err:", a);
    });
  },
})